package com.base.core.action.keywords;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.base.core.BaseAction;
import com.base.core.util.NetUtil;
import com.base.core.util.StringUtil;

@SuppressWarnings("serial")
public class SearchBaiduShouluWeightAction extends BaseAction {

	private String searchDomain="";

	private String analysisResult = "";
	private String keywordInfo = "";

	public String getAnalysisResult() {
		return analysisResult;
	}

	public void setAnalysisResult(String analysisResult) {
		this.analysisResult = analysisResult;
	}

	public String getKeywordInfo() {
		return keywordInfo;
	}

	public void setKeywordInfo(String keywordInfo) {
		this.keywordInfo = keywordInfo;
	}

	public String getSearchDomain() {
		return searchDomain.replaceAll("\\/", "");
	}

	public void setSearchDomain(String searchDomain) {
		this.searchDomain = searchDomain;
	}
	
	public String input(){
		return SUCCESS;
	}

	public String execute() {
		if(StringUtil.isEmpty(this.getSearchDomain())){
			addActionError("请先输入你要查询的地址");
			return SUCCESS;
		}
		this.getSearchResult();
		return SUCCESS;
	}

	private void getSearchResult() {
		String results = "";
		results =new NetUtil().getTargetStr(
				"http://www.aizhan.com/baidu/" + this.getSearchDomain()+"/",
				"utf-8");
		Pattern pattern = Pattern
				.compile("(.*)<div class=\"box_02\">(.*)<div class=\"box_02_r\">(.*)");
		Matcher matcher = pattern.matcher(results.toString());
		if (matcher.find()) {
			analysisResult = matcher.group(2);
		}
		pattern = Pattern
				.compile("(.*)<tr class=\"tablehead\">(.*)</td>\\s+</tr>\\s+</table>\\s+</div>(.*)");
		matcher = pattern.matcher(results.toString());
		if (matcher.find()) {
			keywordInfo = matcher.group(2);
		}
	}

	public static void main(String[] args) {
		String results = new NetUtil().getTargetStr(
				"http://www.aizhan.com/baidu/www.ubao.com/", "utf-8");
		String result1 = "", result2 = "";
		Pattern pattern = Pattern
				.compile("(.*)<div class=\"box_02\">(.*)<div class=\"box_02_r\">(.*)");
		Matcher matcher = pattern.matcher(results.toString());
		if (matcher.find()) {
			result1 = matcher.group(2);
		}
		System.out.println(result1.trim());
		pattern = Pattern
				.compile("(.*)<tr class=\"tablehead\">(.*)</td>\\s+</tr>\\s+</table>\\s+</div>(.*)");
		matcher = pattern.matcher(results.toString());
		if (matcher.find()) {
			result2 = matcher.group(2);
		}

		System.out.println(result2.trim());
	}

}
